set_property(DIRECTORY . PROPERTY FOLDER "CSSEng")

include(PurCCommon)
include(MyMacros.cmake)

configure_file(include/csseng-version.h.in ${FORWARDING_HEADERS_DIR}/csseng/csseng-version.h @ONLY)

set(CSSEng_PRIVATE_INCLUDE_DIRECTORIES
    "${CMAKE_BINARY_DIR}"
    "${CSSENG_DIR}"
    "${CSSENG_DIR}/include"
    "${CSSEng_DERIVED_SOURCES_DIR}"
    "${FORWARDING_HEADERS_DIR}"
)

# the headers should be installed to system here.
set(CSSEng_FRAMEWORK_HEADERS
    "${CSSENG_DIR}/include/csseng-computed.h"
    "${CSSENG_DIR}/include/csseng-errors.h"
    "${CSSENG_DIR}/include/csseng-fontface.h"
    "${CSSENG_DIR}/include/csseng-fpmath.h"
    "${CSSENG_DIR}/include/csseng-functypes.h"
    "${CSSENG_DIR}/include/csseng-hint.h"
    "${CSSENG_DIR}/include/csseng-parserutils.h"
    "${CSSENG_DIR}/include/csseng-properties.h"
    "${CSSENG_DIR}/include/csseng-select.h"
    "${CSSENG_DIR}/include/csseng-stylesheet.h"
    "${CSSENG_DIR}/include/csseng-types.h"
    "${CSSENG_DIR}/include/csseng-wapcaplet.h"
    "${CSSENG_DIR}/include/csseng.h"
)

set(CSSEng_INSTALLED_HEADERS ${CSSEng_FRAMEWORK_HEADERS})
list(APPEND CSSEng_INSTALLED_HEADERS
    "${FORWARDING_HEADERS_DIR}/csseng/csseng-version.h"
)

# TODO: List the directorires which do not contain any platform-dependent C source files
set(CSSEng_PLATFORM_INDEPENDENT_DIRS
    "wapcaplet"
    "parser-charset"
    "parser-input"
    "parser-utils"
    "bytecode"
    "charset"
    "lex"
    "parse"
    "select"
    "utils"
)

# This macro will append all C/CXX source files in ${CSSEng_PLATFORM_INDEPENDENT_DIRS}
# to ${CSSEng_SOURCES}
APPEND_ALL_SOURCE_FILES_IN_DIRLIST(CSSEng_SOURCES "${CSSEng_PLATFORM_INDEPENDENT_DIRS}")

# TODO: List the source files individually.
list(APPEND CSSEng_SOURCES
)

# Put the CXX source files which can be unified in `Sources.txt`
list(APPEND CSSEng_UNIFIED_SOURCE_LIST_FILES
    "Sources.txt"
)

add_custom_command(
    OUTPUT "${CSSEng_DERIVED_SOURCES_DIR}/select/autogenerated_computed.h"
           "${CSSEng_DERIVED_SOURCES_DIR}/select/autogenerated_propget.h"
           "${CSSEng_DERIVED_SOURCES_DIR}/select/autogenerated_propset.h"
           "${CSSEng_DERIVED_SOURCES_DIR}/select/autogenerated_foo.c"
    MAIN_DEPENDENCY "${CODE_TOOLS_DIR}/select/select_generator.py"
    DEPENDS "${CODE_TOOLS_DIR}/select/select_generator.py"
            "${CODE_TOOLS_DIR}/select/select_config.py"
            "${CODE_TOOLS_DIR}/select/overrides.py"
            "${CODE_TOOLS_DIR}/select/assets.py"
    COMMAND ${Python3_EXECUTABLE} "${CODE_TOOLS_DIR}/select/select_generator.py" "${CSSEng_DERIVED_SOURCES_DIR}/select"
    COMMAND ${CMAKE_COMMAND} -E touch "${CSSEng_DERIVED_SOURCES_DIR}/select/autogenerated_foo.c"
    COMMENT "Generating headers for select by using ${Python3_EXECUTABLE}"
    WORKING_DIRECTORY "${CODE_TOOLS_DIR}/select"
    VERBATIM)
list(APPEND CSSEng_SOURCES "${CSSEng_DERIVED_SOURCES_DIR}/select/autogenerated_foo.c")

file(STRINGS "parse/properties.in" lines)
foreach(line IN LISTS lines)
    if (line STREQUAL "")
        continue()
    endif ()

    if (NOT ((${line} MATCHES "^#") OR (${line} MATCHES "^$")))
        string(REPLACE ":" ";" fields ${line})
        list(GET fields 0 property)
        add_custom_command(
            OUTPUT "${CSSEng_DERIVED_SOURCES_DIR}/parse/autogenerated_${property}.c"
            MAIN_DEPENDENCY "${CODE_TOOLS_DIR}/parser/css_property_parser_gen.c"
            DEPENDS "${CSSENG_DIR}/parse/properties.in"
            COMMAND ${CMAKE_COMMAND} -E make_directory "${CSSEng_DERIVED_SOURCES_DIR}/parse"
            COMMAND "${CMAKE_BINARY_DIR}/Source/CodeTools/parser/csspropgen" -o "${CSSEng_DERIVED_SOURCES_DIR}/parse/autogenerated_${property}.c" "${line}"
            COMMENT "Generating source to parse `${property}` by using csspropgen"
            WORKING_DIRECTORY "${CODE_TOOLS_DIR}/parser"
            VERBATIM)
        list(APPEND CSSEng_SOURCES "${CSSEng_DERIVED_SOURCES_DIR}/parse/autogenerated_${property}.c")
    endif ()
endforeach(line)

set(CSSEng_LIBRARIES)
set(CSSEng_DEPENDENCIES)

set(CSSEng_INTERFACE_LIBRARIES CSSEng)
set(CSSEng_INTERFACE_INCLUDE_DIRECTORIES ${CSSEng_FRAMEWORK_HEADERS_DIR})

PURC_FRAMEWORK_DECLARE(CSSEng)
PURC_INCLUDE_CONFIG_FILES_IF_EXISTS()

PURC_WRAP_SOURCELIST(${CSSEng_SOURCES})

PURC_COPY_FILES(CSSEng_CopyHeaders
    DESTINATION ${CSSEng_FRAMEWORK_HEADERS_DIR}/csseng
    FILES ${CSSEng_FRAMEWORK_HEADERS}
    FLATTENED
)
list(APPEND CSSEng_INTERFACE_DEPENDENCIES CSSEng_CopyHeaders)

# CSSEng_CopyHeaders needs to have a direct or indirect
# dependency of CSSEng target for CMake Visual Studio generator to
# eliminate duplicated custom commands. Otherwise, CSSPropertyNames.h
# will be generated in both projects.
if (NOT INTERNAL_BUILD)
    add_dependencies(CSSEng_CopyHeaders CSSEng)
    add_dependencies(CSSEng csspropgen)
    add_dependencies(CSSEng WTF)
endif ()

PURC_COMPUTE_SOURCES(CSSEng)
PURC_FRAMEWORK(CSSEng)
PURC_FRAMEWORK_TARGET(CSSEng)

# The -ftree-sra optimization (implicit with -O2) causes crashes when
# allocating large chunks of memory using bmalloc on Intel 32bit.
# See https://bugs.webkit.org/show_bug.cgi?id=146440
string(TOLOWER ${CMAKE_HOST_SYSTEM_PROCESSOR} LOWERCASE_CMAKE_HOST_SYSTEM_PROCESSOR)
if (CMAKE_COMPILER_IS_GNUCXX AND "${LOWERCASE_CMAKE_HOST_SYSTEM_PROCESSOR}" MATCHES "(i[3-6]86|x86)$")
    PURC_ADD_TARGET_CXX_FLAGS(CSSEng -fno-tree-sra)
endif ()

if (${CSSEng_LIBRARY_TYPE} MATCHES "SHARED")
    set_target_properties(CSSEng PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR})
    install(TARGETS CSSEng DESTINATION "${LIB_INSTALL_DIR}")
endif ()

if (MSVC)
    set_target_properties(CSSEng PROPERTIES
        COMPILE_PDB_NAME ${CSSEng_OUTPUT_NAME}
    )
endif ()

# add_subdirectory(res)

install(TARGETS CSSEng
        DESTINATION "${LIB_INSTALL_DIR}/"
)

install(FILES ${CSSEng_INSTALLED_HEADERS}
        DESTINATION "${HEADER_INSTALL_DIR}/csseng"
)
